import axios from 'axios'
import { API_BASE_URL } from '@/constants/index'

axios.defaults.baseURL = API_BASE_URL
const instance = axios.create({ timeout: 10 * 1000 })
instance.defaults.headers.get['Content-Type'] = 'application/json'
instance.defaults.headers.post['Content-Type'] = 'application/json'

instance.interceptors.request.use(
  (config) => {
    // config.headers['X-Auth-Token'] = token.get();
    return config
  },
  (error) => Promise.reject(error)
)

instance.interceptors.response.use(
  (res) => {
    if (res.status >= 200 && res.status < 300) {
      return Promise.resolve(res)
    }
    return Promise.reject(res)
  },
  (error) => Promise.reject(error)
)

export function get(path: string, params = {}) {
  return instance.get(path, { params })
}

export function put(path: string, params = {}) {
  return instance.put(path, params)
}

export function post(path: string, params = {}) {
  return instance.post(path, params)
}

export function del(path: string, params = {}) {
  return instance.delete(path, params)
}

export default instance
